<?php
// 本类由系统自动生成，仅供测试用途
namespace Backend\Controller;
use MyBase\BaseController;
use MyBase\Tool;
class FoodController extends BaseController {
    //获取商品添加的页面
    public function ajaxGetAddFoodPage(){
        layout(false);
        $goodsTypeRes = $this->GoodsTypeModel->getGoodsTypeList();
        $this->assign('goods_type_list',$goodsTypeRes);
        $this->display("Index/food_add");
    }
    //异步添加商品
    public function ajaxAddFood(){
        $postData = I("post.");
        //将图片拼成json串
        $foodPicArr = array();
        if(!empty($postData['food_img_one'])){
            $foodPicArr['one'] = $postData['food_img_one'];

        }
        if(!empty($postData['food_img_two'])){
            $foodPicArr['two'] = $postData['food_img_two'];
        }
        if(!empty($postData['food_img_three'])){
            $foodPicArr['three'] = $postData['food_img_three'];
        }
        unset($postData['food_img_one']);
        unset($postData['food_img_two']);
        unset($postData['food_img_three']);
        $postData['food_pic'] = json_encode($foodPicArr);
        $postData['food_add_time'] = time();
        foreach ($postData as $key => $value) {
            $key_ = str_replace('food', 'goods', $key);
            $postData[$key_] = $value;
            unset($postData[$key]);
        }
        $res = $this->GoodsModel->addGoods($postData);
        if(!$res){
            $this->ajaxReturn(false,'添加失败!');
        }
        //转移图片
        $IndexController = A("Index");
        foreach ($foodPicArr as $key => $value) {

            $moveRes = $IndexController->movePic($value,$postData['goods_add_time'],$res,'food');
            if(!$moveRes){
                $this->GoodsModel->deleteGoods($res);
                $this->ajaxReturn(false,'图片丢失，请重传图片!');
            }
        }
        $this->ajaxReturn(true,'添加成功!');
    }
    //商品列表分页页面
    public function ajaxLoadFoodListPage(){
        layout(false);
        $foodPageLimit = 3;
        $pageNum = I("get.");//page_num
        $getData = I("post.");//获取用来查询的数据
        // var_dump($getData);
        $where = array();
        if(!empty($getData['t'])){
            $where['goods_type'] = array('eq',$getData['t']);
            $this->assign('t',$getData['t']);
        }else{
            $where['goods_type'] = array('in',array(1,2,3));
        }
        if(!empty($getData['c'])){
            $where['goods_name'] = array('like',"%".$getData['c']."%");
            $this->assign('c',$getData['c']);
        }
        // print_r($getData);
        $count = $this->GoodsModel->where($where)->count();//获取总的记录数
        // echo $count;
        // print_r($getData);
        $Page = new \Think\Page($count,$foodPageLimit);
        $show = $Page->show();//页码
        $data = $this->GoodsModel->where($where)->page($pageNum['p'],$foodPageLimit)->select();
        // echo count($data);
        foreach ($data as $key => $value) {
            $picArr = json_decode($value['goods_pic'],true);
            foreach ($picArr as $k => $v) {
                $picArr[$k] =  __ROOT__.'/Uploads/food/'.$data[$key]['goods_add_time'].$data[$key]['goods_id'].'/'.$v;
            }
            $data[$key]['goods_pic'] = $picArr;

        }
        // print_r($data);
        $this->assign('food_data',$data);
        $this->assign('food_page',$show);

        //获取food_search的分类
        $goodsTypeRes = $this->GoodsTypeModel->getGoodsTypeList();
        $this->assign('goods_search_type',$goodsTypeRes);
        $this->display("Index/food_list_page");
    }
    //异步删除商品记录
    public function ajaxFoodDelete(){
        $postData = I('post.');
        // print_r($postData);exit;
        $res = $this->GoodsModel->deleteGoods($postData['goods_id']);
        if($res === false){
            $this->ajaxReturn(false,'删除失败！');
        }
        $this->ajaxReturn(true,'删除成功！',$postData['goods_id']);
    }
    //异步禁用商品
    public function ajaxFoodForbidden(){
        $postData = I('post.');
        $res = $this->GoodsModel->forbiddenAndRecoverGoods($postData['goods_id'],"N");
        if($res === false){
            $this->ajaxReturn(false,'更新失败！');
        }
        $this->ajaxReturn(true,'更新成功！',$postData['goods_id']);
    }
    //异步恢复商品
    public function ajaxFoodRecover(){
        $postData = I('post.');
        $res = $this->GoodsModel->forbiddenAndRecoverGoods($postData['goods_id'],"Y");
        if($res === false){
            $this->ajaxReturn(false,'更新失败！');
        }
        $this->ajaxReturn(true,'更新成功！',$postData['goods_id']);
    }

    //异步推荐商品
    public function ajaxFoodRecOrUnRec(){
        $postData = I('post.');
        // print_r($postData);
        $res = $this->GoodsModel->recommentAndUnRecGoods($postData['goods_id'],$postData['recomment_data']);
        if($res === false){
            $this->ajaxReturn(false,'操作失败！');
        }
        $this->ajaxReturn(true,'操作成功！',$postData['goods_id']);        
    }
    //获取编辑页面
    public function ajaxGetFoodEditPage(){
        layout(false);
        //取数据
        $postData = I('post.');
        $res_ = $this->GoodsModel->getGoodsInfoById($postData['goods_id']);
        if(!$res_){
            $this->error("非法操作");
        }
        $res = $res_[0];
        $picArr = json_decode($res['goods_pic'],true);
        foreach ($picArr as $k => $v) {
                $picArr[$k] =  __ROOT__.'/Uploads/food/'.$res['goods_add_time'].$res['goods_id'].'/'.$v;
            }
        $res['goods_pic'] = $picArr;
        $this->assign('edit_data',$res);

        $goodsTypeRes = $this->GoodsTypeModel->getGoodsTypeList();
        $this->assign('goods_type_list',$goodsTypeRes);

        $this->display("Index/food_edit_page");
    }
    //异步保存编辑
    public function ajaxSaveFoodEdit(){
        $postData = I('post.');
        //获取商品的信息
        $oldData = $this->GoodsModel->getGoodsInfoById($postData['food_edit_id']);
        if(!$oldData){
            $this->ajaxReturn(false,'非法操作');
        }
        $picOldArr = json_decode($oldData['goods_pic'],true);
        $picSaveArr = array();//这个装最后存入数据的数据
        $picNewArr = array();//这个装新上传的图片名称，方便移动图片
        foreach ($postData as $key => $value) {
            if(strpos($key, "food_edit_img_") !== false){
                //去后面的英文one two three
                $key_ = $eNum = str_replace("food_edit_img_", "", $key);
                if(!empty($value)){
                    $picSaveArr[$key_] = $value;
                    $picNewArr[] = $value;
                }else{
                    if(!empty($picOldArr[$key_])){
                         $picSaveArr[$key_] = $picOldArr[$key_];
                     }
                }
                unset($postData["food_edit_img_".$key_]);
            }
        }
        //转移图片
        $IndexController = A("Index");
        if(count($picNewArr) > 0){
            foreach ($picNewArr as $k => $v) {
                $IndexController->movePic($v,$oldData['goods_add_time'],$oldData['goods_id'],"food");
            }
        }
        $postData['food_edit_pic'] = json_encode($picSaveArr);
        //换掉数组中的food_edit这个字符串
        foreach ($postData as $key => $value) {
            $saveKey = str_replace('food_edit', 'goods', $key);
            $postData[$saveKey] = $value;
            unset($postData[$key]);
        }
        $res = $this->GoodsModel->updateGoods($postData);
        if($res === false){
            $this->ajaxReturn(false,'更新失败！');
        }
        $this->ajaxReturn(true,'更新成功！');
    }
}